package lero.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lero.pojo.Admin;
import lero.pojo.Role;
import lero.vo.RoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{

    //定义一个全局的日志记录器，通过LoggerFactory获取
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    /**
     *功能描述:新增文章
     * @date 2018/7/16
     * @param role ：pojo实体
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/insert.do")
    @ResponseBody
    public HashMap insert(@ModelAttribute("role") Role role){
        roleServiceI.insert(role);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    @RequestMapping("/select.do")
    @ResponseBody
    public  PageInfo<Role> select(@RequestBody RoleVo vo){
        PageHelper.startPage(vo.getCurrentPage(),vo.getRows());
        List<Role> roles = roleServiceI.select(vo);
        PageInfo<Role> info = new PageInfo<>(roles);
        return info;
    }

    /**
     *功能描述:删除单条数据
     * @date 2018/7/16
     * @param id
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public  HashMap delete(@RequestParam("id") int id){
        roleServiceI.delete(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    /**
     *功能描述:批量删除
     * @date 2018/7/16
     * @param ids  批量删除数据的id组成的数组
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/deleteMany.do")
    @ResponseBody
    public  HashMap deleteMany(@RequestParam("ids[]") int[] ids){
        roleServiceI.deleteMany(ids);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述:
     * @date 2018/7/10
     * @param id : 查询文章的id
     * @return lero.pojo.Role
     */
    @RequestMapping("/selectOne.do")
    @ResponseBody
    public  Role selectOne(@RequestParam("id") int id){
        Role role = roleServiceI.selectOne(id);
        return role;
    }


    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ResponseBody
    public  HashMap update(@ModelAttribute("role") Role role){
        roleServiceI.update(role);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述: 查询不在当前角色的启用的用户
     * @date 2018/7/19
     * @param id  角色ID
     * @return java.util.HashMap
     */
    @RequestMapping(value="/selectAdminNotInCurrentRole.do")
    @ResponseBody
    public  HashMap selectAdminNotInCurrentRole(@RequestParam("id") int id){
        List<Admin> admins = roleServiceI.selectAdminNotInCurrentRole(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        map.put("code",0);
        map.put("data",admins);
        return map;
    }

    /**
     *功能描述: 查询当前角色启用用户
     * @date 2018/7/19
     * @param id  角色ID
     * @return java.util.HashMap
     */
    @RequestMapping(value="/selectAdminInCurrentRole.do")
    @ResponseBody
    public  HashMap selectAdminInCurrentRole(@RequestParam("id") int id){
        List<Admin> admins = roleServiceI.selectAdminInCurrentRole(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        map.put("code",0);
        map.put("data",admins);
        return map;
    }

    /**
     *功能描述: 查询所有的启用用户
     * @date 2018/7/19
     * @param
     * @return java.util.HashMap
     */
    @RequestMapping(value="/selectAllAdmin.do")
    @ResponseBody
    public  HashMap selectAllAdmin(){
        List<Map> admins = roleServiceI.selectAllAdmin();
        HashMap map = new HashMap();
        map.put("code",0);
        map.put("data",admins);
        return map;
    }


    @RequestMapping(value="/saveAdminIntoCurrentRole.do")
    @ResponseBody
    public  HashMap saveAdminIntoCurrentRole(@RequestParam("ids") String ids,@RequestParam("id") int id){
        //当前角色没有分配任何权限
        if(StringUtils.isEmpty(ids.trim())){
            roleServiceI.deleteAdminByCurrentRole(id);
        }else{
            String[] split = ids.split(",");
            int[] ints = new int[split.length];
            for (int i=0;i<ints.length;i++) {
                ints[i]= Integer.parseInt(split[i]);
            }
            roleServiceI.saveAdminIntoCurrentRole(ints,id);
        }
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


}
